package com.ly.fn.biz.ins.srv.api.register;

import com.ly.fn.biz.ins.srv.api.annotation.InsAutoApi;
import com.ly.fn.biz.ins.srv.api.common.ServiceConfig;
import com.ly.fn.biz.ins.srv.api.processor.ControllerProcessor;
import com.ly.fn.inf.config.ctx.conf.SystemEnv;
import com.ly.fn.inf.util.FileUtil;
import com.ly.fn.inf.util.StringUtil;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;

import java.io.File;

import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;

/**
 * @Author xiaoyuan.wang
 * @Date 2021/9/27 10:35
 * @Version 1.0
 */
public class SrvServiceBeanRegister implements ImportBeanDefinitionRegistrar {

    private final static String projectConfigBasePath = "/.app_config/";

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean enabled = true;

        String basePackage = "", exportClzPath = "", env = "";
        boolean mqsEnabled = false;

        AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(InsAutoApi.class.getName()));

        if (annotationAttributes != null) {
            enabled = annotationAttributes.getBoolean("enable");
            basePackage = annotationAttributes.getString("basePackage");

            exportClzPath = annotationAttributes.getString("exportPath");
            env = annotationAttributes.getString("env");

            mqsEnabled = annotationAttributes.getBoolean("mqsEnabled");
        }

        if (!"integration".equals(SystemEnv.getEnv()) &&
                !"dev".equals(SystemEnv.getEnv()) &&
                !"stable".equals(SystemEnv.getEnv()) &&
                !"qa".equals(SystemEnv.getEnv())) {
            enabled = false;
        }

        if (enabled) {
            System.out.println(("current Env :" + SystemEnv.getEnv() + ". auto gen srv api start."));
            //自动检测本地配置文件目录
            String userDir = System.getProperty("user.home");
            String configDir = userDir + projectConfigBasePath + SystemEnv.getSystemId();
            File directory = new File(configDir);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            String configEnv = configDir + "/env";
            File envFile = new File(configEnv);
            if (StringUtil.isNotBlank(env)) {
                FileUtil.writeFile(envFile,env);
            }

            if (StringUtils.isEmpty(basePackage)) {
                String className = importingClassMetadata.getClassName();
                int index = className.lastIndexOf(".");
                basePackage = className.substring(0, index);
            }

            System.setProperty("mqs.rebalance.wait.time.seconds","2");
            System.setProperty("disable.mqs.attach","true");

            ServiceConfig.instance.setBasePackage(basePackage);
            ServiceConfig.instance.setExportClzPath(exportClzPath);
            ServiceConfig.instance.setMqsEnabled(mqsEnabled);
            BeanDefinitionBuilder builderController = rootBeanDefinition(ControllerProcessor.class);
            builderController.addConstructorArgValue(basePackage);
            builderController.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
            AbstractBeanDefinition beanDefinitionController = builderController.getBeanDefinition();
            BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinitionController, registry);
        }
    }
}